﻿using System.Linq;
using MadMediaManager.Core.Domain;
using NUnit.Framework;
using SharpTestsEx;

namespace MadMediaManager.DataAccess.Tests.Repositories
{
    [TestFixture]
    public class SoftwareIsoRepositoryTest : BaseRepositoryTest
    {
        [Test]
        public void Test_Properties_Mapped()
        {
            var iso = SoftwareIso.NewIso("path", "name", 5);
            iso.Architecture = Architecture.X86;
            iso.Platform = Platform.Windows;
            iso.ProductFamily = "family";
            iso.Publisher = "publisher";
            iso.Rating = Rating.Three;
            iso.Version = "7.0";
            iso.Year = 2005;
            iso.IsDeleted = true;

            iso.RowVersion.Should().Be.Null();

            DbContext.SoftwareIsos.Save(iso);
            FlushAndClear();

            iso.RowVersion.Should().Not.Be.Null();

            SoftwareIso found = DbContext.SoftwareIsos.Get(iso.EntityId);

            found.FileSize.Unit.Should().Be.EqualTo(iso.FileSize.Unit);
            found.FileSize.Value.Should().Be.EqualTo(iso.FileSize.Value);
            found.Name.Should().Be.EqualTo(iso.Name);
            found.Architecture.Should().Be.EqualTo(iso.Architecture);
            found.Platform.Should().Be.EqualTo(iso.Platform);
            found.ProductFamily.Should().Be.EqualTo(iso.ProductFamily);
            found.Publisher.Should().Be.EqualTo(iso.Publisher);
            found.Rating.Should().Be.EqualTo(iso.Rating);
            found.Version.Should().Be.EqualTo(iso.Version);
            found.Year.Should().Be.EqualTo(iso.Year);
            found.IsoType.Should().Be.EqualTo(iso.IsoType);
            found.RowVersion.Should().Have.SameSequenceAs(iso.RowVersion);
            found.IsDeleted.Should().Be.EqualTo(iso.IsDeleted);
        }

        [Test]
        public void AddLicense_SoftwareLicense_SavesToDatabase()
        {
            var iso = SoftwareIso.NewIso("path", "name", 5);
            iso.AddLicense(new SoftwareLicense{Key = "key", Machine = "machine"});

            DbContext.SoftwareIsos.Save(iso);
            FlushAndClear();

            var found = DbContext.SoftwareIsos.Get(iso.EntityId);
            found.Licenses.Should().Not.Be.Empty();
            found.Licenses.First().Key.Should().Be.EqualTo("key");
            found.Licenses.First().Machine.Should().Be.EqualTo("machine");
        }

        [Test]
        public void RemoveLicense_Genre_DeletesFromDatabase()
        {
            var iso = SoftwareIso.NewIso("path", "name", 5);
            var license = new SoftwareLicense {Key = "key", Machine = "machine"};
            iso.AddLicense(license);

            DbContext.SoftwareIsos.Save(iso);
            DbContext.SubmitChanges();

            iso.RemoveLicense(license);
            FlushAndClear();

            var found = DbContext.SoftwareIsos.Get(iso.EntityId);
            found.Licenses.Should().Be.Empty();
        }
    }
}
